package com.ruoyi.system.Fdongbei.service.dibang;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.Fdongbei.domain.dibang.*;

import com.ruoyi.system.Fdongbei.domain.vo.CarInfoListResVo;
import com.ruoyi.system.Fdongbei.domain.vo.CarTotalListResVo;
import com.ruoyi.system.Fdongbei.domain.vo.reqvo.CarInfoReqVo;
import com.ruoyi.system.Fdongbei.domain.vo.reqvo.CarTotalInfoVo;
import com.ruoyi.system.Fdongbei.domain.vo.reqvo.XiangXiInfo;
import com.ruoyi.system.Fdongbei.domain.vo.resvo.*;
import com.ruoyi.system.Fdongbei.mapper.dibang.GuozhongDoMapper;
import com.ruoyi.system.Fdongbei.mapper.dibang.HuayanDoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@DataSource(value = DataSourceType.LY002_L)
public class CarInfoServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(CarInfoServiceImpl.class);
    @Resource
    private GuozhongDoMapper guozhongDoMapper;

    @Resource
    private HuayanDoMapper huayanDoMapper;

    /*
    散户玉米查询
     */


    public AjaxResult selectCornCarShInfo(CarInfoReqVo carInfoReqVo) {
//        Date beginTime = carInfoReqVo.getBeginTime();
//        Date endTime = carInfoReqVo.getEndTime();
//        String xcbm = carInfoReqVo.getXcbm();
//        String chehao = carInfoReqVo.getChehao();
//        Integer pageNum = carInfoReqVo.getPageNum();
//        Integer pageSize = carInfoReqVo.getPageSize();
//        pageNum = (pageNum - 1) * pageSize;
//        carInfoReqVo.setPageNum(pageNum);
        List<CarInfoVo> cornCarShInfo = guozhongDoMapper.getCornCarShInfo1(carInfoReqVo);

        carInfoReqVo.setPageNum(0);
        List<CarInfoVo> cornCarShInfo1 = guozhongDoMapper.getCornCarShInfo1(carInfoReqVo);
        if (Objects.isNull(cornCarShInfo) || cornCarShInfo.size() <= 0) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = cornCarShInfo.stream().map(CarInfoVo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        int total = cornCarShInfo1.size();
        for (CarInfoVo carInfoVo : cornCarShInfo) {
            String czbh = carInfoVo.getCzbh();
            HuayanDo huayanDo = huayanDoMap.get(czbh);
            BigDecimal sf = huayanDo.getSf();
            BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
            carInfoVo.setSf(sf);
            carInfoVo.setKz(kz);
        }
//        for(GuozhongDo guozhongDo:guozhongDos){
//            CornCarInfoResVo carResVo = new CornCarInfoResVo();
//            String czbh = guozhongDo.getCzbh();
//            BigDecimal sf = huayanDoMapper.selectByCzbh(czbh);//查询出化验水分
//            BigDecimal kz = guozhongDo.getJz().subtract(guozhongDo.getJszl());
//            carResVo.setKz(kz);
//            carResVo.setSf(sf);
//            carResVo.setGuozhongDo(guozhongDo);
//            cornCarInfos.add(carResVo);
//        }
        CarInfoListResVo carInfoListResVo = new CarInfoListResVo();
        carInfoListResVo.setTotal(total);
        carInfoListResVo.setCarInfoVos(cornCarShInfo);
        return AjaxResult.success(carInfoListResVo);
    }

    /*
    玉米-贸易粮查询
     */
    public AjaxResult selectCornCarTradeInfo(CarInfoReqVo carInfoReqVo) {

        CarInfoListResVo carInfoListResVo = new CarInfoListResVo();
        List<CarInfoVo> carInfoVos = guozhongDoMapper.getCornCarTradeInfo(carInfoReqVo);
        carInfoReqVo.setPageNum(0);
        List<CarInfoVo> carInfoVos1 =  guozhongDoMapper.getCornCarTradeInfo(carInfoReqVo);
        int total = carInfoVos1.size();
        if (Objects.isNull(carInfoVos)) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = carInfoVos.stream().map(CarInfoVo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        for (CarInfoVo carInfoVo : carInfoVos) {
            String czbh = carInfoVo.getCzbh();
            HuayanDo huayanDo = huayanDoMap.get(czbh);
            BigDecimal sf = null;
            if(Objects.nonNull(huayanDo)){
                sf = huayanDo.getSf();
            }
            BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
            carInfoVo.setSf(sf);
            carInfoVo.setKz(kz);

        }
        carInfoListResVo.setCarInfoVos(carInfoVos);
        carInfoListResVo.setTotal(total);
        return AjaxResult.success(carInfoListResVo);
    }

    /**
     * 玉米中储粮信息查询
     *
     * @param carInfoReqVo
     * @return
     */
    public AjaxResult selectCornCarSinotransInfo(CarInfoReqVo carInfoReqVo) {
        CarInfoListResVo carInfoListResVo = new CarInfoListResVo();
        List<CarInfoVo> carInfoVos = guozhongDoMapper.getCornCarSinotransInfo(carInfoReqVo);
        carInfoReqVo.setPageNum(0);
        List<CarInfoVo> carInfoVos1 =  guozhongDoMapper.getCornCarTradeInfo(carInfoReqVo);
        int total = carInfoVos1.size();
        if (Objects.isNull(carInfoVos)) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = carInfoVos.stream().map(CarInfoVo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        for (CarInfoVo carInfoVo : carInfoVos) {
            String czbh = carInfoVo.getCzbh();
            HuayanDo huayanDo = huayanDoMap.get(czbh);
            BigDecimal sf = null;
            if(Objects.nonNull(huayanDo)){
                sf = huayanDo.getSf();
            }
            BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
            carInfoVo.setSf(sf);
            carInfoVo.setKz(kz);

        }
        carInfoListResVo.setCarInfoVos(carInfoVos);
        carInfoListResVo.setTotal(total);
        return AjaxResult.success(carInfoListResVo);
    }

    /**
     * 煤炭汽运
     * @param carInfoReqVo
     * @return
     */
    public AjaxResult selectCoalCarInfo(CarInfoReqVo carInfoReqVo) {
        Date beginTime = carInfoReqVo.getBeginTime();
        Date endTime = carInfoReqVo.getEndTime();
        String xcbm = carInfoReqVo.getXcbm();
        String chehao = carInfoReqVo.getChehao();
        String gysdm = carInfoReqVo.getGysdm();
        List<CarInfoResVo> coalCarInfos = new ArrayList<>();
        List<GuozhongDo> guozhongDos = guozhongDoMapper.getCoalCarInfo(beginTime, endTime, xcbm, chehao, gysdm);
        if (Objects.isNull(guozhongDos) || guozhongDos.size() <= 0) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = guozhongDos.stream().map(GuozhongDo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        if(Objects.isNull(huayanDoMap) || huayanDoMap.size() <= 0) {
            List<CarInfoResVo> carInfoResVos = guozhongDos.stream().map(guozhongDo -> {
                CarInfoResVo carInfoResVo = new CarInfoResVo();
                carInfoResVo.setGuozhongDo(guozhongDo);
                return carInfoResVo;
            }).collect(Collectors.toList());
            return AjaxResult.success(carInfoResVos);
        }
        for (GuozhongDo guozhongDo : guozhongDos) {
            String czbh = guozhongDo.getCzbh();
            CarInfoResVo carInfoResVoResVo = new CarInfoResVo();
            carInfoResVoResVo.setGuozhongDo(guozhongDo);
            BigDecimal kz = guozhongDo.getJz().subtract(guozhongDo.getJszl());
            carInfoResVoResVo.setKz(kz);
            HuayanDo huayanDo = huayanDoMap.get(czbh);
            if(Objects.nonNull(huayanDo)){
                BigDecimal sf = huayanDo.getSf();
                carInfoResVoResVo.setSf(sf);
            }
            coalCarInfos.add(carInfoResVoResVo);
        }
        return AjaxResult.success(coalCarInfos);
    }

    /**
     * 煤炭铁路
     * @param carInfoReqVo
     * @return
     */
    public AjaxResult selectCoalTrainInfo(CarInfoReqVo carInfoReqVo) {
        List<CarInfoResVo> coalCarInfos = new ArrayList<>();
        List<GuozhongDo> guozhongDos = guozhongDoMapper.getCoalTrainInfo(carInfoReqVo);
        if (Objects.isNull(guozhongDos) || guozhongDos.size() <= 0) {
            return AjaxResult.error("未查询到数据");
        }
        for (GuozhongDo guozhongDo : guozhongDos) {
            CarInfoResVo carInfoResVoResVo = new CarInfoResVo();
            carInfoResVoResVo.setGuozhongDo(guozhongDo);
            BigDecimal kz = guozhongDo.getJz().subtract(guozhongDo.getJszl());
            carInfoResVoResVo.setKz(kz);
            coalCarInfos.add(carInfoResVoResVo);
        }
        return AjaxResult.success(coalCarInfos);
    }

    /**
     * 查询外销出库数据
     * @param carInfoReqVo
     * @return
     */
    public AjaxResult selectExportInfo(CarInfoReqVo carInfoReqVo){
        List<CarInfoVo> exportInfo = guozhongDoMapper.getExportInfo(carInfoReqVo);

        carInfoReqVo.setPageNum(0);
        List<CarInfoVo> cornCarShInfo1 = guozhongDoMapper.getExportInfo(carInfoReqVo);
        if (Objects.isNull(exportInfo) || exportInfo.size() <= 0) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = exportInfo.stream().map(CarInfoVo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        int total = cornCarShInfo1.size();
        for (CarInfoVo carInfoVo : exportInfo) {
            String czbh = carInfoVo.getCzbh();
            HuayanDo huayanDo = huayanDoMap.get(czbh);
            BigDecimal sf = null;
            if(Objects.nonNull(huayanDo)){
                 sf = huayanDo.getSf();
            }
            BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
            carInfoVo.setSf(sf);
            carInfoVo.setKz(kz);
        }
        CarInfoListResVo carInfoListResVo = new CarInfoListResVo();
        carInfoListResVo.setTotal(total);
        carInfoListResVo.setCarInfoVos(exportInfo);
        return AjaxResult.success(carInfoListResVo);
    }


    //
    public AjaxResult selectCarInfo(CarInfoReqVo carInfoReqVo){
        CarInfoListResVo carInfoListResVo = new CarInfoListResVo();
        List<CarInfoVo> carInfoVos = guozhongDoMapper.getCarInfo(carInfoReqVo);
        Integer total = guozhongDoMapper.getSum(carInfoReqVo);
        if (Objects.isNull(carInfoVos)) {
            return AjaxResult.error("未查询到相关数据");
        }
        List<String> czbhs = carInfoVos.stream().map(CarInfoVo::getCzbh).collect(Collectors.toList());
        Map<String, HuayanDo> huayanDoMap = null;
        if (Objects.nonNull(czbhs) && !czbhs.isEmpty()) {
            huayanDoMap = huayanDoMapper.selectByCzbhs(czbhs);
        }
        if(Objects.nonNull(huayanDoMap)){
            for (CarInfoVo carInfoVo : carInfoVos) {
                String czbh = carInfoVo.getCzbh();
                HuayanDo huayanDo = huayanDoMap.get(czbh);
                BigDecimal sf = null;
                if(Objects.nonNull(huayanDo)){
                    sf = huayanDo.getSf();
                }
                BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
                carInfoVo.setSf(sf);
                carInfoVo.setKz(kz);
            }
        }else{
            for (CarInfoVo carInfoVo : carInfoVos) {
                BigDecimal kz = carInfoVo.getJz().subtract(carInfoVo.getJszl());
                carInfoVo.setKz(kz);
            }
        }
        carInfoListResVo.setCarInfoVos(carInfoVos);
        carInfoListResVo.setTotal(total);
        return AjaxResult.success(carInfoListResVo);
    }

    public AjaxResult updateGuozhong(GuozhongDo guozhongDo){
        int i = guozhongDoMapper.updateByPrimaryKeySelective(guozhongDo);
        return AjaxResult.success(i);
    }

    @DataSource(value = DataSourceType.LY002_L)
    public AjaxResult selectTotalInfo(CarTotalInfoVo carTotalInfoVo){
        System.out.println(carTotalInfoVo.toString());
        List<CarTotalInfoResVo> totalInfo = guozhongDoMapper.getTotalInfo(carTotalInfoVo);
        Integer totalSum = guozhongDoMapper.getTotalSum(carTotalInfoVo);
        CarTotalListResVo carTotalListResVo = new CarTotalListResVo();
        carTotalListResVo.setCarTotalInfoVoList(totalInfo);
        carTotalListResVo.setTotal(totalSum);
        return AjaxResult.success(carTotalListResVo);
    }

    public AjaxResult selectXiangXiInfo(XiangXiInfo xiangXiInfo){
        XiangXiListInfo list = new XiangXiListInfo();
        List<XiangXiResVo> xiangXiList = guozhongDoMapper.getXiangXiInfo(xiangXiInfo);
        Integer xiangxiTotal = guozhongDoMapper.getXiangxiTotal(xiangXiInfo);
        list.setList(xiangXiList);
        list.setTotal(xiangxiTotal);
        return AjaxResult.success(list);
<<<<<<< HEAD
=======

>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    }

    public AjaxResult selectXiangXiAllInfo(String czbh){
        XiangXiAllResVo xiangxiInfo = guozhongDoMapper.getByPrimaryKey(czbh);
        HuayanDo huayanDo = huayanDoMapper.selectByPrimaryKey(czbh);
        return AjaxResult.success(xiangxiInfo);
    }
<<<<<<< HEAD
=======

>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27

}


